package com.yyx.app1.Question_function.Control.ChatListView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.yyx.app1.Data.BaseDataManager;
import com.yyx.app1.Data.Question.Question_Activity_Data;
import com.yyx.app1.Data.testDate;
import com.yyx.app1.MyApp;
import com.yyx.app1.R;
import com.yyx.app1.tool.ImageTool;
import com.yyx.app1.ui.ImageShower_Activity;

import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

/**
 * Created by yuexing on 2015/10/20.
 */
@EViewGroup(R.layout.itemview_chat_head)
public class ItemVIew_Chat_Head extends LinearLayout {
    private Context context;
    @ViewById
    ImageView itemview_chat_head_portrait;

    @ViewById
    TextView itemview_chat_head_name,itemview_chat_head_content,itemview_chat_head_time;

    @ViewById
    LinearLayout itemview_chat_head_imagelist;

    public ItemVIew_Chat_Head(Context context) {
        super(context);
        this.context = context;
    }

    public ItemVIew_Chat_Head(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public ItemVIew_Chat_Head(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    public void bind(Question_Activity_Data data)
    {
        try {
            ImageLoader imageLoader = BaseDataManager.getMyImageLoader(context);
            imageLoader.get(data.getPortraitURL(), imageLoader.getImageListener(itemview_chat_head_portrait, testDate.loadImageRes,testDate.defaultPortrait), ImageTool.dip2px(context, 40), ImageTool.dip2px(context, 40));
            Bundle tag = new Bundle();
            tag.putString("type", "URL");
            tag.putString("value", data.getPortraitURL());
            ImageShower_Activity.setTagOnClickListener(itemview_chat_head_portrait, context, tag);
            itemview_chat_head_name.setText(data.getUsername());
            itemview_chat_head_content.setText(data.getConditionsdescribed());
            itemview_chat_head_time.setText(data.getTime());
            //添加图片
            itemview_chat_head_imagelist.removeAllViews();
            double imagecount = data.getImagelistURL().length;
            if (imagecount == 1)
            {
                ImageView imageView=new ImageView(context);
                imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                imageView.setLayoutParams(new ViewGroup.LayoutParams(ImageTool.getScreenSize(context).x - ImageTool.dip2px(context, 90), ViewGroup.LayoutParams.WRAP_CONTENT));
                imageView.setPadding(ImageTool.dip2px(context, 10), ImageTool.dip2px(context, 10), ImageTool.dip2px(context, 10), ImageTool.dip2px(context, 10));
                imageLoader.get(data.getImagelistURL()[0], imageLoader.getImageListener(imageView, testDate.loadImageRes,testDate.defaultMedicationImage)
                        , ImageTool.getScreenSize(context).x - ImageTool.dip2px(context, 110), ImageTool.dip2px(context, 230));
                Bundle tag1 = new Bundle();
                tag1.putString("type", "URL");
                tag1.putString("value", data.getImagelistURL()[0]);
                ImageShower_Activity.setTagOnClickListener(imageView, context, tag1);
                itemview_chat_head_imagelist.addView(imageView);
            }
            else
            {
                for (double i=0;i< imagecount/3.0 ;++i)
                {
                    int linewidth = ImageTool.getScreenSize(context).x - ImageTool.dip2px(context, 90);
                    int padding = ImageTool.dip2px(context,5);
                    int imagewidth = (linewidth-2*padding)/3;
                    LinearLayout linearLayout = new LinearLayout(context);
                    linearLayout.setPadding(0, padding/2, 0, padding/2);
                    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    linearLayout.setOrientation(HORIZONTAL);
                    itemview_chat_head_imagelist.addView(linearLayout);
                    for (int j=0; j<3 ; ++j)
                    {
                        ImageView imageView=new ImageView(context);
                        imageView.setLayoutParams(new ViewGroup.LayoutParams(imagewidth,imagewidth));
                        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        imageView.setBackground(new ColorDrawable(Color.RED));
                        Bundle tag1 = new Bundle();
                        tag1.putString("type", "URL");
                        tag1.putString("value", data.getImagelistURL()[(int) i * 3 + j]);
                        ImageShower_Activity.setTagOnClickListener(imageView, context, tag1);
                        imageLoader.get(data.getImagelistURL()[(int) i * 3 + j], imageLoader.getImageListener(imageView, testDate.loadImageRes,testDate.defaultMedicationImage)
                                , imagewidth,imagewidth);
                        linearLayout.addView(imageView);
                        if ((i*3+j+1)==data.getImagelistURL().length)
                        {
                            break;
                        }
                        if (j!=2)
                        {
                            ImageView divider = new ImageView(context);
                            divider.setLayoutParams(new LinearLayout.LayoutParams(padding, imagewidth));
                            linearLayout.addView(divider);
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            Log.e("异常1510201746",e.toString());
        }
    }
}
